package base.loader
{
	import flash.events.Event;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.utils.ByteArray;

	public class URLLoaderBase extends BaseLoader
	{
		private var _isSharedCache:Boolean;
		
		public function URLLoaderBase(loaderFlagType:String, id:int, url:String, isSharedCache:Boolean, repeat:int=3, urlVariables:URLVariables=null, requestMethod:String=URLRequestMethod.GET, loaderVersion:int=0, level:int=0)
		{
			_isSharedCache=isSharedCache;
			super(loaderFlagType, id, url, repeat, urlVariables, requestMethod, loaderVersion, level);
		}
		
		override protected function __onCompleted(evt:Event):void
		{
			if(_isSharedCache) LoaderShared.Instance.cacheFile(_url, _urlLoader.data);
			super.__onCompleted(evt);
		}
		
		/**
		 * 执行加载(如果直接执行加载,其加载将不会载入加载列表,也将会忽略自身在缓存中的已加载项)
		 */
		override public function executeGo():void
		{
			if(_isComplete)
			{//如果已经加载成功过，直接发出完成事件
				loaderComplete();
				return;
			}
			if(_isLoading) return;//如果在加载进行中，则返回
			
			if(_isSharedCache)
			{
				var ba:ByteArray=LoaderShared.Instance.getCachedFile(_url);
				if(ba)
				{
					loadByBytes(ba);
				}
				else
				{
					super.executeGo();
				}
			}
			else
			{
				super.executeGo();
			}
		}
		
		private function loadByBytes(bytes:ByteArray):void
		{
			_urlLoader.data=bytes;
			super.__onCompleted(null);
		}
		
		override public function clone():BaseLoader
		{
			var urlLoaderBase:URLLoaderBase=new URLLoaderBase(_loaderFlagType, _id, _url, _isSharedCache,_repeat,_urlVariables, _requestMethod, _loaderVersion);
			urlLoaderBase.isComplete=_isComplete;
			urlLoaderBase.isSuccess=_isSuccess;
//			urlLoaderBase.isLoading=_isLoading;
			urlLoaderBase.progress=_progress;
			return urlLoaderBase;
		}
		
		override public function dispose():void
		{
			super.dispose();
		}
	}
}